﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Crawler.Model;
using Husb.Framework.DataAccess;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace Crawler.DataAccess
{
    public partial class TaskCategoryDao : DataAccessBase<TaskCategoryInfo>
    {
        public TaskCategoryDao()
        {
            //ObjectId = "Id";
            //MasterObjectId = "";
            //DetailObjectId = "";
            CommandType = System.Data.CommandType.StoredProcedure;

            GetByIdStatement = "Crawler.WebPages_SelectWebPage";
            //GetByNameStoredProc = SR.SelectWebPageByName;
            FindAllStatement = "Crawler.WebPages_SelectWebPagesAll";
            //FindStoredProc = SR.SelectWebPagesDynamic;
            //FindByNameStoredProc = SR.SelectWebPagesByNameInexact;


            InsertStatement = "Crawler.WebPages_InsertWebPage";
            UpdateStatement = "Crawler.WebPages_UpdateWebPage";
            //CreateUpdateStoredProc = SR.InsertUpdateWebPage;
            //DeleteByIdStoredProc = SR.DeleteWebPage;
            //DeleteByObjectStoredProc = SR.DeleteWebPage;

            //GetRowCountStoredProc = SR.SelectWebPagesRowCount;
            //GetPagedSortedProc = SR.SelectWebPagesPaged;
        }

        #region PopulateParamters
        /// <summary>
        /// 根据实体类的属性，给数据库命令的参数赋值
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="cmd">命令对象</param>
        /// <param name="taskCategory"></param>
        protected override void PopulateParamters(TaskCategoryInfo taskCategory, Database db, System.Data.Common.DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, taskCategory.Id);
            db.AddInParameter(cmd, "Name", DbType.String, taskCategory.Name);
            db.AddInParameter(cmd, "ParentId", DbType.Guid, taskCategory.ParentId);
        }
        #endregion

        #region  LoadProperty
        /// <summary>
        /// 根据数据库的查询结果，给实体类的属性赋值
        /// </summary>
        /// <param name="taskCategory"></param>
        /// <param name="dr"><see cref="IDataReader" />类的一个实例</param>
        protected override void LoadProperty(TaskCategoryInfo taskCategory, System.Data.IDataReader dr)
        {
            taskCategory.Id = (System.Guid)dr["Id"];
            taskCategory.Name = dr["Name"] == DBNull.Value ? "" : (System.String)dr["Name"];
            taskCategory.ParentId = dr["ParentId"] == DBNull.Value ? Guid.Empty : (System.Guid?)dr["ParentId"];
        }
        #endregion

        #region LoadHalfTaskCategoryInfo
        /// <summary>
        /// 根据执行不同的数据获取命令，得到一个不完整的TaskCategoryInfo<see cref="TaskCategoryInfo" />对象，也许只有部分属性有内容。
        /// </summary>
        /// <param name="taskCategory"></param>
        /// <param name="dr">IDataReader<see cref="IDataReader" /></param>
        public static void LoadHalfTaskCategoryInfo(TaskCategoryInfo taskCategory, System.Data.IDataReader dr)
        {
            for (int fieldCount = 0; fieldCount < dr.FieldCount; fieldCount++)
            {
                switch (dr.GetName(fieldCount))
                {
                    case "Id":
                        taskCategory.Id = (System.Guid)dr["Id"];
                        break;
                    case "Name":
                        taskCategory.Name = dr["Name"] == DBNull.Value ? "" : (System.String)dr["Name"];
                        break;
                    case "ParentId":
                        taskCategory.ParentId = dr["ParentId"] == DBNull.Value ? Guid.Empty : (System.Guid?)dr["ParentId"];
                        break;
                }
            }
        }
        #endregion
    }
}
